package com.tools;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bean.*;
import com.tools.timetask.DataMockHelper;

import java.sql.*;
import java.text.DecimalFormat;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.time.LocalDateTime;
import java.time.ZoneId;


public class Main {
    public static double srcDeep ;

    public static void main(String[] args) {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

        Runnable task = new Runnable() {
            @Override
            public void run() {
                SlDataDTO slDataDTO = selectSlDataDTO();
                DTO2DataConverter converter = new DTO2DataConverter();
                SignsDataBO bo = converter.convert(slDataDTO);
                System.out.println("转换后的数据:"+bo);
                List<String> blobTOs = WXDatahubSingleton.getInstance().generateBlobTOs(bo);
                WXDatahubSingleton.getInstance().sendBlob(blobTOs);
            }
        };
        long initialDelay = 0; // Initial delay in seconds
        long period = 10; // Interval between executions in minutes
        // 调度任务
        scheduler.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.MINUTES);
    }

    public static void handleTest(){
        String timeDetail = "[{\"time\":\"2024-08-22 10:15:00\",\"val\":1710.22000},"
                + "{\"time\":\"2024-08-22 10:05:00\",\"val\":1710.20000},"
                + "{\"time\":\"2024-08-22 09:55:00\",\"val\":1710.20000},"
                + "{\"time\":\"2024-08-22 09:45:00\",\"val\":1710.19000},"
                + "{\"time\":\"2024-08-22 09:35:00\",\"val\":1710.18000},"
                + "{\"time\":\"2024-08-22 09:25:00\",\"val\":1710.20000}]";
        List<TimeDTO> list = JSON.parseObject(timeDetail, new TypeReference<List<TimeDTO>>(){});
        for (TimeDTO obj : list) {
            System.out.println("Time: " + obj.getTime() + ", Value: " + obj.getVal());
        }
    }





    public static SlDataDTO selectSlDataDTO() {
        Connection c = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        SlDataDTO dataDTO = new SlDataDTO();
        try {
            // 连接数据库
            Class.forName("org.postgresql.Driver");
            c = DriverManager.getConnection(
                    "jdbc:postgresql://23.159.250.160:5432/bmzyc",
                    "wsdsjzx",
                    "563%ymwdCg@z");
            c.setAutoCommit(false); // 手动管理事务
            System.out.println("连接数据库成功！");
            String sql1 = "SELECT *\n" +
                    "\tFROM wsslj.cy_jsc_water_regime\n" + "where equipment_name='千丈岩水库'" +
                    "\torder by last_time desc;";
            statement = c.prepareStatement(sql1);
            rs = statement.executeQuery();
            rs.next();
            String row_id = rs.getString(1);
            String equipment_name = rs.getString(3);
            String val = rs.getString(5);
            String warn_threshold = rs.getString(7);
            String last_time = rs.getString(9);
            String detail = rs.getString(14);
            System.out.println("当前数据为:");
            System.out.println(row_id + ',' + equipment_name + ',' + val + ',' + last_time + ',' + warn_threshold+','+detail);
            rs.close();
            statement.close();
            c.close();
            String value = val;
            dataDTO.setName(equipment_name);
            dataDTO.setCurDeep(value);
            dataDTO.setTimes(last_time);
            dataDTO.setDetail(detail);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
            c.close();
        } finally {
            return dataDTO;
        }
    }


}